package Day05;

import java.lang.invoke.LambdaConversionException;
import java.util.Arrays;

public class Text4 {
    public static void main(String[] args) {
        //二分查找法找下标
        int[] a = {1,6,5,3,8,2,20,13};
        Arrays.sort(a);
        System.out.print("{");
        for (int i = 0; i < a.length; i++) {
            System.out.print(a[i] +",");
            if(i == a.length -1){
                System.out.print("}");
            }
        }
        System.out.println();
        System.out.println(bing(a, 20));

    }


    public static int bing(int[] a, int key){
        int left = 0;
        int right = a.length- 1;
        while(left <= right){
            int mid = (left + right) /2;
            if(a[mid] < key){
                left = mid + 1;
            }else if(a[mid] > key){
                right = mid -1;
            }else{
                return mid;
            }
        }
        return -1;
    }
}
